package com.lwj.liwanjia.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.bean.AllMoneyBean;
import com.lwj.liwanjia.impl.AllMoneyView;
import com.lwj.liwanjia.presenter.AllMoeyPresenter;

/**
 * Created by Administrator on 2017/9/15.
 */

public class PayDialog extends Dialog implements View.OnClickListener, AllMoneyView {
    private Context context;
    private double price;//总价格
    private RelativeLayout payYikatong;
    private TextView tvYikatong;
    private TextView tvZhifubao;
    private TextView tvWeixin;
    private RelativeLayout payZhifubao;
    private RelativeLayout payWeixin;
    private ImageView ivQuxiao, ivYikatong, ivZhifubao, ivWeixin;
    private TextView tvPay;//确认付款
    private int checkPosition = 0;
    private double allMoney;//获取可用的总余额
    private AllMoeyPresenter allMoeyPresenter;

    public PayDialog(Context context, double tvPrice) {
        super(context, R.style.MyDialog);
        this.context = context;
        this.price = tvPrice;
        allMoeyPresenter = new AllMoeyPresenter(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_layout);
        allMoeyPresenter.getAllMoey(SpfUtils.getSpfUtils(context).getToken());
        initView();
        initClick();
    }

    private void initClick() {
        payYikatong.setOnClickListener(this);
        payWeixin.setOnClickListener(this);
        payZhifubao.setOnClickListener(this);
        ivQuxiao.setOnClickListener(this);
    }

    private void initView() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.dialog_animation);
        WindowManager.LayoutParams params = window.getAttributes();
        setCanceledOnTouchOutside(false);
        params.width = params.MATCH_PARENT;
        params.height = params.WRAP_CONTENT;
        window.setAttributes(params);
        ivQuxiao = (ImageView) findViewById(R.id.iv_quxiao);
        ivYikatong = (ImageView) findViewById(R.id.iv_yikatong);
        ivZhifubao = (ImageView) findViewById(R.id.iv_zhifubao);
        ivWeixin = (ImageView) findViewById(R.id.iv_weixin);
        TextView tvPrice = (TextView) findViewById(R.id.tv_price);
        tvPrice.setText("￥" + price + "元");
        payYikatong = (RelativeLayout) findViewById(R.id.pay_yikatong);
        tvYikatong = (TextView) findViewById(R.id.tv_yikatong);
        tvZhifubao = (TextView) findViewById(R.id.tv_zhifubao);
        tvPay = (TextView) findViewById(R.id.tv_pay);
        tvWeixin = (TextView) findViewById(R.id.tv_weixin);
        payZhifubao = (RelativeLayout) findViewById(R.id.pay_zhifubao);
        payWeixin = (RelativeLayout) findViewById(R.id.pay_weixin);
    }

    public int getCheckPosition() {
        return checkPosition;
    }

    public TextView getPay() {
        return tvPay;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pay_weixin://微信支付：
                checkPosition = 2;
                ivWeixin.setImageResource(R.mipmap.zhifu);
                ivZhifubao.setImageResource(R.mipmap.zhifu_1);
                ivYikatong.setImageResource(R.mipmap.zhifu_1);
                tvWeixin.setText("￥" + price + "元");
                tvWeixin.setVisibility(View.VISIBLE);
                tvZhifubao.setVisibility(View.GONE);
                if (allMoney < price) {//余额不足
                    tvYikatong.setText("余额不足");
                } else {
                    tvYikatong.setVisibility(View.GONE);
                }
                break;
            case R.id.pay_yikatong://一卡通支付
                checkPosition = 0;
                ivWeixin.setImageResource(R.mipmap.zhifu_1);
                ivZhifubao.setImageResource(R.mipmap.zhifu_1);
                ivYikatong.setImageResource(R.mipmap.zhifu);
                tvYikatong.setText("￥" + price + "元");
                tvWeixin.setVisibility(View.GONE);
                tvZhifubao.setVisibility(View.GONE);
                tvYikatong.setVisibility(View.VISIBLE);
                break;
            case R.id.pay_zhifubao://支付宝支付
                checkPosition = 1;
                ivWeixin.setImageResource(R.mipmap.zhifu_1);
                ivZhifubao.setImageResource(R.mipmap.zhifu);
                ivYikatong.setImageResource(R.mipmap.zhifu_1);
                tvZhifubao.setText("￥" + price + "元");
                tvWeixin.setVisibility(View.GONE);
                tvZhifubao.setVisibility(View.VISIBLE);
                if (allMoney < price) {//余额不足
                    tvYikatong.setText("余额不足");
                } else {
                    tvYikatong.setVisibility(View.GONE);
                }
                break;
            case R.id.iv_quxiao://取消支付
                PayCancleDialog cancleDialog = new PayCancleDialog(context, PayDialog.this);
                cancleDialog.show();
                break;
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(context, "访问网络失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getAllMoney(AllMoneyBean allMoneyBean) {
        if (allMoneyBean.getState() == 0) {
            Toast.makeText(context, "获取可用余额失败", Toast.LENGTH_SHORT).show();
        } else if (allMoneyBean.getState() == 1) {
            allMoney = allMoneyBean.getData().getAllMoney();
            if (allMoney < price) {//余额不足
                payYikatong.setClickable(false);
                checkPosition = 1;
                ivWeixin.setImageResource(R.mipmap.zhifu_1);
                ivZhifubao.setImageResource(R.mipmap.zhifu);
                ivYikatong.setImageResource(R.mipmap.zhifu_1);
                tvZhifubao.setText("￥" + price + "元");
                tvWeixin.setVisibility(View.GONE);
                tvZhifubao.setVisibility(View.VISIBLE);
                tvYikatong.setText("余额不足");
                payYikatong.setBackgroundColor(context.getResources().getColor(R.color.colorF2F2F2));
            } else {
                tvYikatong.setText("￥" + price + "元");
                payYikatong.setBackgroundColor(context.getResources().getColor(R.color.colorWite));
            }
        }
    }


    @Override
    public void setOnDismissListener(@Nullable OnDismissListener listener) {
        super.setOnDismissListener(listener);
        if (allMoeyPresenter != null) {
            allMoeyPresenter = null;
        }
    }
}
